package com.agritech.smartfarming.model;

import com.agritech.smartfarming.strategy.TerrainAwarePathStrategy;

/**
 * 播种机类，继承自农机设备基类
 */
public class Seeder extends AgriculturalMachine {
    private double seedCapacity; // 种子容量 (kg)
    private double seedingRate; // 播种速率 (kg/ha)
    private String seedType; // 种子类型
    
    public Seeder(String id, String name) {
        super(id, name, "播种机");
        this.seedCapacity = 1000.0;
        this.seedingRate = 120.0;
        this.seedType = "小麦";
        // 默认使用地形感知路径规划策略
        this.pathStrategy = new TerrainAwarePathStrategy();
    }
    
    @Override
    public void performTask() {
        if (status == MachineStatus.WORKING) {
            System.out.println(name + " 正在执行播种任务，种子类型: " + seedType + ", 播种速率: " + seedingRate + " kg/ha");
        } else {
            System.out.println(name + " 当前不在工作状态，无法执行任务");
        }
    }
    
    @Override
    public void update(EnvironmentData environmentData) {
        System.out.println(name + " 接收到环境数据更新");
        
        // 根据环境数据调整播种参数
        if (environmentData.getSoilMoisture() < 20) {
            System.out.println("⚠️ " + name + " 检测到土壤湿度过低 (" + environmentData.getSoilMoisture() + 
                "%)，增加播种深度，确保种子获得足够水分");
        } else if (environmentData.getSoilMoisture() > 80) {
            System.out.println("⚠️ " + name + " 检测到土壤湿度过高 (" + environmentData.getSoilMoisture() + 
                "%)，暂停播种操作，避免种子腐烂");
            if (status == MachineStatus.WORKING) {
                status = MachineStatus.IDLE;
                System.out.println(name + " 自动暂停播种作业");
            }
        }
        
        // 根据土壤pH值调整播种策略
        if (environmentData.getSoilPH() < 5.5) {
            System.out.println("⚠️ " + name + " 检测到土壤pH值过低 (" + environmentData.getSoilPH() + 
                ")，建议先进行土壤改良再播种");
        }
        
        // 更新位置信息
        if (environmentData.getPosition() != null) {
            double distance = calculateDistance(this.position, environmentData.getPosition());
            if (distance < 100) {  // 如果环境数据采集点在农机100米范围内
                System.out.println(name + " 更新当前区域环境参数，调整播种深度和密度");
            }
        }
    }
    
    /**
     * 计算两点之间的距离
     */
    private double calculateDistance(Position p1, Position p2) {
        double latDiff = p2.getLatitude() - p1.getLatitude();
        double longDiff = p2.getLongitude() - p1.getLongitude();
        return Math.sqrt(latDiff * latDiff + longDiff * longDiff) * 111000;
    }
    
    /**
     * 设置种子类型
     */
    public void setSeedType(String seedType) {
        this.seedType = seedType;
        System.out.println(name + " 已更换种子类型为: " + seedType);
    }
    
    /**
     * 获取播种机特有信息
     */
    public String getSeederInfo() {
        return String.format("种子容量: %.1f kg, 播种速率: %.1f kg/ha, 种子类型: %s", 
            seedCapacity, seedingRate, seedType);
    }
} 